import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class TCPClient extends JFrame {
    private JProgressBar progressBar;
    private JTextArea textArea, textArea_1;
    private JScrollPane scrollPane;
    private JScrollPane scrollPane_1;
    private JTextField portTxt;
    private Socket s;
    private JTextField ipTxt;

    public TCPClient() {
        getContentPane().setLayout(null);
        JLabel lblNewLabel = new JLabel("TCP\u901A\u4FE1\u529F\u80FD\u754C\u9762(Client)");
        lblNewLabel.setForeground(Color.BLUE);
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        lblNewLabel.setBounds(44, 10, 364, 36);
        getContentPane().add(lblNewLabel);
        JButton btnNewButton = new JButton("\u8FDE\u63A5\u4E0B\u4F4D\u673A");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int n = Integer.parseInt(portTxt.getText());
                    String ip = ipTxt.getText();
                    s = new Socket(ip, n);
                    new Thread() {
                        public void run() {
                            InputStreamReader in;
                            try {
                                in = new InputStreamReader(s.getInputStream());
                                char chars[] = new char[64];
                                int len;
                                while (true) {
                                    while ((len = in.read(chars)) != -1) {
                                        String msg = new String(chars, 0, len);
                                        switch (msg.charAt(0)) {
                                            case 'A':
                                                textArea.append(msg.substring(1) + "\n");
                                                progressBar.setValue((int) Float.parseFloat(msg.substring(1)));
                                                Object GlobalVar;
                                                if (GlobalVar.canPlay) {
                                                    MusicUtil.getInstance().play(msg.substring(1));
                                                }
                                                break;
                                            case 'B':
                                                textArea_1.append(msg.substring(1) + "\n");
                                                break;
                                        }
                                    }
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }.start();

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
}
